import { defineStore } from 'pinia'

import { ref, reactive } from 'vue'
// 设计一个缓存 取色器
import { provinceApiGet } from "@/api/common"
export const useColorStore = defineStore('colorStoreItem', () => {
    const count = ref<number>(22)
    const province = ref()
    const selectProvinceCity = ref()
    const selectCityArea = ref()
    const twoLevelCity = ref()
    const ispSelectList = ref<any[]>([
        { key: "ChinaMobile", value: "中国移动", id: 1 },
        { key: "ChinaUnicom", value: "中国联通", id: 2 },
        { key: "ChinaTelecom", value: "中国电信", id: 3 },
        { key: "ChinaBroadnet", value: "中国广电", id: 4 },
        { key: "Other", value: "其他", id: 5 }
    ])


    const setColorId = (item: { name: string, id: number }) => {
        count.value = item.id
    }
    const getProvince = async () => {
        const res = await provinceApiGet()
        console.log();

        province.value = res.data.dictItemList[0].childrenDictItemList
        twoLevelCity.value = keepNodesAtLevel(province.value, 2)
    }
    // type = 1 查询 省或者直辖市的下一级 type = 2 市的下一级区域 接受当前的id 和 type 类型
    const filterData = (type: number, id: string | null, code?: string) => {
        if (type === 1) {
            // 根据code去查
            if (code) {
                let arr = province.value.filter(item => {

                    if (item.dictItemCode == code) {
                        selectProvinceCity.value = item.childrenDictItemList
                        return item
                    }
                })
                return arr
            } else {
                // 根据Id 去查
                province.value.some((item: any) => {
                    if (item.id == id) {
                        selectProvinceCity.value = item.childrenDictItemList
                        return
                    }
                })
            }


        } else if (type === 2) {
            selectProvinceCity.value.some((item: any) => {
                if (item.id == id) {
                    selectCityArea.value = item.childrenDictItemList
                    return
                }
            })
        }
    }
    const keepNodesAtLevel = (data: any, level: number) => {
        if (level == 1) {
            return data.map((node) => {
                return {
                    dictCode: node.dictCode,
                    dictItemCode: node.dictItemCode,
                    id: node.id,
                    dictItemName: node.dictItemName
                }
            })
        }
        let result = []
        for (let i = 0; i < data.length; i++) {
            let node = data[i]
            /**
             * dictCode: "PROVINCE_CITY_AREA"
             * dictItemCode: "11"
                  dictItemName: "北京市"
                  id: 100002
                  childrenDictItemList
             */
            if (node.childrenDictItemList) {
                node.childrenDictItemList = keepNodesAtLevel(node.childrenDictItemList, level - 1)
            }
            result.push(node)
        }
        return result
    }
    return {
        count,
        province,
        selectProvinceCity,
        selectCityArea,
        setColorId,
        getProvince,
        filterData,
        keepNodesAtLevel,
        twoLevelCity,
        ispSelectList
    }
},
    {
        persist: {
            storage: sessionStorage,
            key: 'colorStoreItem',
            paths: ['province', 'twoLevelCity'],
        },
    }
)

